import { getMyProblemPiecePageApi } from "@/api/approval/myApproval";
import dayjs from "dayjs";
export default function OperationHook(stateList: Base.GeneraDataType) {
  const { searchForm, tableData, drawerOpts, loading } = stateList;
  const getData = () => {
    const params = {
      ...getParams()
    };

    loading.value = true;
    return getMyProblemPiecePageApi(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.total = Number(data.total);
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };

  const getParams = () => {
    const { waybillNumList, problemPieceTypeList, stateList, createByList, time, beforePageNum, pageNum, pageSize, customerIdList } = searchForm;
    const params: any = {
      waybillNumList: [],
      customerIdList,
      problemPieceTypeList,
      stateList,
      createByList,
      createStartTime: time.length ? dayjs(time[0]).format("YYYY-MM-DD HH:mm:ss") : undefined,
      createEndTime: time.length ? dayjs(time[1]).format("YYYY-MM-DD HH:mm:ss") : undefined,
      beforePageNum,
      pageNum,
      pageSize
    };
    if (waybillNumList)
      params.waybillNumList = waybillNumList
        .trim()
        .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
        .split(",");
    return params;
  };

  const searchHandle = () => {
    searchForm.pageNum = 1;
    getData();
  };
  //编辑问题件类型
  const auditorHandle = (row: CountryRegion.ListRows) => {
    drawerOpts.title = "编辑问题件类型";
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.component = "ProblemShipment";
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };

  //查看问题件
  const checkProblemHandle = (row: CountryRegion.ListRows) => {
    drawerOpts.title = "查看问题件";
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.component = "CheckProblem";
    drawerOpts.passData = row;
    drawerOpts.visible = true;
  };

  return {
    searchHandle,
    getData,
    auditorHandle,
    checkProblemHandle
  };
}
